package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"new/kpg/app"
	"new/kpg/e"
	"new/models"
	"new/services"
)
func GetCustomers(c *gin.Context)  {
	//page := -1
	//if arg := c.Query("page"); arg != "" {
	//	page = com.StrTo(arg).MustInt()
	//}
	//limit := -1
	//if arg := c.Query("limit"); arg != "" {
	//	limit = com.StrTo(arg).MustInt()
	//}
	//searchName := ""
	//if arg := c.Query("searchName"); arg != "" {
	//	searchName = arg
	//}
	//supplierParam := map[string]interface{}{
	//	"page": page,
	//	"limit": limit,
	//	"searchName": searchName,
	//}
	err, info, total := services.GetAllCustomers()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

// DelCustomers 删除顾客
func DelCustomers(c *gin.Context){
	id := -1
	if arg := c.PostForm("CustomersCno"); arg != "" {
		id = com.StrTo(arg).MustInt()
	}
	if id == -1{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.DelCustomers(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateCustomers(c *gin.Context){
	//b, _ := c.GetRawData()  //获取原始字节
	//var m map[string]string
	//_ = json.Unmarshal(b, &m)  //将结构类型转换为json字符串
	id :=c.PostForm("CustomersCno")
	id2 := com.StrTo(id).MustInt()
	name:=c.PostForm("CustomersCname")
	Cbesom:= c.PostForm("CustomersCbesom")
	Phonenum:=c.PostForm("CustomersPhonenum")
	if id == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	//purchaseDetailsGoods := -1
	//purchaseDetailsGoods = com.StrTo(m["purchaseDetailsGoods"]).MustInt()
	//purchaseDetailsNumber := -1
	//purchaseDetailsNumber = com.StrTo(m["purchaseDetailsNumber"]).MustInt()
	//purchaseDetailsList := -1
	//purchaseDetailsList = com.StrTo(m["purchaseDetailsList"]).MustInt()
	//purchaseDetailsRemarks := m["purchaseDetailsRemarks"]
	//err2, price := services.GetGoodPriceById(purchaseDetailsGoods)
	//price = price * purchaseDetailsNumber
	//if err2 !=nil{
	//	app.Error(c,e.ERROR,err2,err2.Error())
	//	return
	//}
	err := services.UpdateCustomers(models.Customers{CustomersCno: id2,CustomersCname:name,CustomersCbesom:Cbesom,CustomersPhonenum:Phonenum})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功")
}
func AddCustomers(c *gin.Context){
	id :=c.PostForm("CustomersCno")
	id2 := com.StrTo(id).MustInt()
	name:=c.PostForm("CustomersCname")
	Cbesom:= c.PostForm("CustomersCbesom")
	Phonenum:=c.PostForm("CustomersPhonenum")
	err := services.AddCustomers(models.Customers{CustomersCno: id2,CustomersCname:name,CustomersCbesom:Cbesom,CustomersPhonenum:Phonenum})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功")
}